import datetime

from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.core.models.enums import UserCalendarStatus
from src.database.models import TimedBaseModel


class UserCalendar(TimedBaseModel):
    __tablename__ = "user_calendar"
    __mapper_args__ = {"eager_defaults": True}

    id: Mapped[int] = mapped_column(primary_key=True)
    status: Mapped[UserCalendarStatus] = mapped_column(server_default="active")
    user = relationship("User", back_populates="calendars")
    user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
    subscription = relationship("Subscription", back_populates="calendars")
    subscription_id: Mapped[int] = mapped_column(ForeignKey("subscription.id"))
    calendar = relationship("Calendar", back_populates="users")
    calendar_id: Mapped[int] = mapped_column(ForeignKey("calendar.id"))
    successful_tasks: Mapped[int] = mapped_column(server_default="0")
    check_tasks_time: Mapped[datetime.time] = mapped_column(server_default="19:00")
    get_tasks_time: Mapped[datetime.time] = mapped_column(server_default="18:00")
    tasks_sent: Mapped[int] = mapped_column(server_default="0")

    def __repr__(self):
        return f"<Календарь №{self.calendar_id}, пользователь №{self.user_id}>"

    def __str__(self):
        return self.__repr__()
